/**
 *
 */
package cn.com.callback

import groovy.json.*

//导入grooby的json解析api包
import java.util.Map;
import cn.com.elite.EliteBean;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import cn.com.elite.Util;

/**
 * @author ThinkPad
 *
 */
public class ReceiveMessage implements CallBackInvoke {
    private static final Log log = LogFactory.getLog(ReceiveMessage.class);

    public void doCallBack(EliteBean eliteBean, String callBack, Map map, String body) {
        def corpid = "301195"; //企业ID
        def loginname = "400"; //登录账号
        def password = "734095"; //登录密码
        def timestamp = map.get("timestamp"); //时间戳
        password = Util.getMD5Str(corpid + password + timestamp); //时间戳
        def url = "http://sms3.mobset.com:8080/Api?wsdl"; //请求URL
        def xmlbody = """<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
	<SOAP-ENV:Body>
		<m:Sms_GetRecv xmlns:m="http://tempuri.org/">
			<m:CorpID>""" + corpid + """</m:CorpID>
			<m:LoginName>""" + loginname + """</m:LoginName>
			<m:Password>""" + password + """</m:Password>
			<m:TimeStamp>""" + timestamp + """</m:TimeStamp>
		</m:Sms_GetRecv>
	</SOAP-ENV:Body>
</SOAP-ENV:Envelope>"""
        log.debug("xmlbody:" + xmlbody);
        def charset = "utf-8";
        def timeout = 100000;
        def SOAPAction = "http://tempuri.org/Sms_GetRecv";
        def httpresult = Util.callWebService(url, xmlbody, charset, SOAPAction, timeout);
        def xml = Util.Transform(httpresult); //去命名空间
        def list = new XmlParser().parseText(xml);
        def responsemsg = list.Body.Sms_GetRecvResponse.SmsRecvList.SmsRecvGroup;
        log.debug("responsemsg:" + responsemsg);
        def updatesql = ""; //申明变量
        def sqlResult = ""; //申明变量
        list.Body.Sms_GetRecvResponse.SmsRecvList.SmsRecvGroup.each {
            try {
                log.debug("it:" + it);
                updatesql = "insert into documentin(documentin_guid,doctype,status,documentpath,documentfrom,documentto,receiveddatetime,handleby_id,handlegroup_id,priority_id,expireddate)values(newid(),3,0,'" + it.Content.text() + "','" + it.Mobile.text() + "',null,getdate(),'SELITE','SYSTEM','LOW',getdate()+30)";
                sqlResult = eliteBean.update(updatesql, 'dataSource');
            } catch (Exception e) {
            }
        };
    }
}